/**
 * DWR proxy for ext grid
 * 
 * @param dwrCall  DWR remote service method call.
 * @param config   configure params 
 * <pre>
 * filter {boolean} default true
 * like {boolean} default true
 * sort {boolean} default true
 * </pre>
 * @author liuhz
 */
CQCIS.data.DWRPageProxy = function(dwrCall, config, ownerCt) {
    config = config || {};
    this.dwrCall = dwrCall;
    this.filter = (config.filter != undefined ? config.filter : true);
    this.like = (config.like != undefined ? config.like : true);
    this.sort = (config.sort != undefined ? config.sort : true);
    this.ownerCt = ownerCt;
    
    CQCIS.data.DWRPageProxy.superclass.constructor.call(this);
};

Ext.extend(CQCIS.data.DWRPageProxy, Ext.data.DataProxy, {
    load : function(params, reader, callback, scope, arg) {
        if (this.fireEvent("beforeload", this, params) !== false) {
            var delegate = this.loadResponse.createDelegate(this, [reader, callback, scope, arg], 1);

            var callParams = new Array();
            if (arg.arg){
                callParams = arg.arg.slice();
            }
            if (this.filter) {
                if (params.filter) {
                    callParams.push(params.filter);
                } else {
                    callParams.push( {});
                }
            }
            if (this.like) {
                if (params.like) {
                    callParams.push(params.like);
                } else {
                    callParams.push( {});
                }
            }
            if (this.sort) {
                var sort = {};
                if (params.sort && params.dir) {
                    sort[params.sort] = params.dir;
                }
                callParams.push(sort);
            }

            Ext.applyIf(params, {start: 0, limit:20});
            callParams.push(Math.ceil(params.start / params.limit) + 1);
            callParams.push(params.limit);
            if (this.ownerCt) {
                var ownerCt = this.ownerCt;
                delegate = {
                    callback: delegate,
                    errorHandler: function(msg, ex){
                        if (ownerCt && ownerCt.loadMask) {
                            ownerCt.loadMask.hide();
                        }
                        if (ownerCt.getEl() && ownerCt.getEl().isMasked()) {
                            ownerCt.getEl().unmask();
                        }
                        if (ownerCt.body && ownerCt.body.isMasked()) {
                            ownerCt.body.unmask();
                        }
                        if (msg && msg.indexOf("登录系统") >= 0) {
                            CQCIS.alert(msg, function(){
                                if (top.userName) {
                                    top.logionWin.init();
                                } else {
                                    top.location.replace(CQCIS.ctxPath + 'index.html');
                                }
                            });
                        } else if (msg) {
                            CQCIS.msg(msg, '远程调用失败');
                            CQCIS.console(msg, 3);
                            if (ex.getDetailedMessage) {
                                CQCIS.console(ex.getDetailedMessage(), 3);
                            } else {
                                CQCIS.console(ex, 3);
                            }
                        }
                    }
                };
            }

            callParams.push(delegate);
            this.dwrCall.apply(this, callParams);
        } else {
            callback.call(scope || this, null, arg, false);
        }
    },

    loadResponse : function(response, reader, callback, scope, arg) {
        var result;
        try {
            result = reader.read(response);
        } catch (e) {
            this.fireEvent("loadexception", this, null, response, e);
            callback.call(scope, null, arg, false);
            return;
        }

        callback.call(scope, result, arg, true);
    },

    update : function(dataSet) {
    },

    updateResponse : function(dataSet) {
    }
});
